/**
 * 
 */
package de.homer.plugins;

import java.io.IOException;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;

import org.xbmc.eventclient.ButtonCodes;
import org.xbmc.eventclient.XBMCClient;

import de.homer.adapter.abstracts.AbstractHomeRPlugin;
import de.homer.adapter.domain.PluginConfiguration;
import de.homer.adapter.domain.PluginMetaInformation;
import de.homer.adapter.interfaces.SimpleMediaControlAdapter;

/**
 * @author André Stannek
 * 
 */
public class SimpleXBMCAdapter extends AbstractHomeRPlugin implements
		SimpleMediaControlAdapter {

	/**
	 * 
	 */
	private final PluginConfiguration configuration;

	/**
	 * 
	 */
	private boolean playing;

	/**
	 * 
	 */
	private static final String HOST = "host";

	/**
	 * 
	 */
	private static final String PORT = "port";

	/**
	 * 
	 */
	public SimpleXBMCAdapter() {
		super();
		configuration = new PluginConfiguration(HOST, PORT);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homer.adapter.interfaces.HomeRPlugin#getMetaInformation()
	 */
	@Override
	public final PluginMetaInformation getMetaInformation() {
		Set<String> attributes = new HashSet<>();
		Set<String> interfaces = new HashSet<>();
		interfaces.add(SimpleMediaControlAdapter.class.getName());
		attributes.add(HOST);
		attributes.add(PORT);
		return new PluginMetaInformation(getClass().getName(), interfaces,
				"XBMC Adapter", attributes);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homer.adapter.interfaces.SimpleMediaControlAdapter#playPause()
	 */
	@Override
	public final void playPause() {
		if (playing) {
			performAction(ButtonCodes.REMOTE_PAUSE);
		} else {
			performAction(ButtonCodes.REMOTE_PLAY);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homer.adapter.interfaces.SimpleMediaControlAdapter#stop()
	 */
	@Override
	public final void stop() {
		performAction(ButtonCodes.REMOTE_STOP);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homer.adapter.interfaces.SimpleMediaControlAdapter#skipForward()
	 */
	@Override
	public final void skipForward() {
		performAction(ButtonCodes.REMOTE_SKIP_PLUS);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homer.adapter.interfaces.SimpleMediaControlAdapter#skipBackward()
	 */
	@Override
	public final void skipBackward() {
		performAction(ButtonCodes.REMOTE_SKIP_MINUS);
	}

	/**
	 * Performs an button press using a {@link XBMCClient}. The button codes are
	 * defined in {@link ButtonCodes}.
	 * 
	 * @param buttonCode
	 *            the button code used in {@link XBMCClient}
	 */
	private void performAction(final String buttonCode) {
		try {
			String ipString = configuration.getAttribute(HOST);
			InetAddress address;
			if (ipString.matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
				String[] ip = ipString.split(".");
				byte[] addr = new byte[] { Byte.parseByte(ip[0]),
						Byte.parseByte(ip[1]), Byte.parseByte(ip[2]),
						Byte.parseByte(ip[3]) };
				address = InetAddress.getByAddress(addr);
			} else {
				address = InetAddress.getByName(ipString);
			}
			int port = Integer.parseInt(configuration.getAttribute(PORT));
			XBMCClient client = new XBMCClient(address, port, "My Client",
					"/usr/share/xbmc/media/icon.png");
			client.sendButton("R1", buttonCode, false, true, true, (short) 0,
					(byte) 0);
			client.stopClient();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homer.adapter.interfaces.HomeRPlugin#getConfiguration()
	 */
	@Override
	public final PluginConfiguration getConfiguration() {
		return configuration;
	}
}
